<?php
namespace app\admin\controller;

use app\common\model\Store as StoreModel;
use app\common\model\StoreSaler as SalerModel;
use app\common\model\StoreOrder as OrderModel;
/**
 * 门店控制器
 */
class Store extends Admin 
{
	/**
	 * 门店列表
	 * @return array
	 */
	public function index(){
		$key = $this->request->param('key');
		$status = $this->request->param('status',-1);

		$where = [];
		$map = [];
		$query = [];
		if(!empty($key)){
			$map[] = ['name','like','%'.$key.'%'];
			$query['key'] = $key;
		}
		if($status >= 0){
			$where['status'] = $status;
			$query['status'] = $status;
		}

		$list = StoreModel::where($map)->where($where)->order('id desc')->paginate(15, false, ['query' => $query]);

		$this->assign('list', $list);
		return $this->fetch();
	}

	/**
	 * 新增门店
	 */
	public function add(){
		if($this->request->isPost()){
			$data = $this->request->post();
			//验证
			$result = $this->validate($data,[
				'name|门店名称' => 'require',
				'realname|联系人' => 'require',
				'mobile|联系电话' => 'require',
				'sale_time|营业时间' => 'require'
			]);
			// 验证失败 输出错误信息
			if(true !== $result) $this->error($result);
			//区域城市
			if(!empty($data['city-picker'])){				
				$area = explode('/',$data['city-picker']);
				$data['province'] = $area[0];
				$data['city'] = $area[1];
				$data['district'] = $area[2];
			}
			unset($data['city-picker']);
			unset($data['provinceId']);
			unset($data['cityId']);
			unset($data['districtId']);

			if($res = StoreModel::create($data)){
				$this->success('新增成功');
			}else{
				$this->error('新增失败');
			}
		}else{
			$this->assign('info', null);

			return $this->fetch('edit');
		}
	}

	/**
	 * 编辑门店
	 */
	public function edit($id = 0){
		if($this->request->isPost()){
			$data = $this->request->post();
			//验证
			$result = $this->validate($data,[
				'name|门店名称' => 'require',
				'realname|联系人' => 'require',
				'mobile|联系电话' => 'require',
				'sale_time|营业时间' => 'require'
			]);
			// 验证失败 输出错误信息
			if(true !== $result) $this->error($result);
			//区域城市
			if(!empty($data['city-picker'])){				
				$area = explode('/',$data['city-picker']);
				$data['province'] = $area[0];
				$data['city'] = $area[1];
				$data['district'] = $area[2];
			}
			unset($data['city-picker']);
			unset($data['provinceId']);
			unset($data['cityId']);
			unset($data['districtId']);

			if($res = StoreModel::update($data)){
				$this->success('编辑成功');
			}else{
				$this->error('编辑失败');
			}
		}else{
			empty($id) && $this->error('参数错误');

			$info = StoreModel::where('id',$id)->find();
			$this->assign('info', $info);

			return $this->fetch();
		}
	}
	
	/**
	 * 删除门店
	 * @param  integer $id 门店ID
	 * @return mixed
	 */
	public function del($id = 0){
		empty($id) && $this->error('参数错误');

		if(is_array($id)){
			$map[] = ['id','in', $id];
		}elseif(is_numeric($id)){
			$map[] = ['id','=',$id];
		}

		if($query = StoreModel::where($map)->delete()){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

	/**
	 * 更新状态
	 * @return mixed
	 */
	public function update(){
		$data = $this->request->param();

		if(empty($data)){
			$this->error('参数错误');
		}	
		
		if($query = StoreModel::update($data)){
			$this->success('更新成功');
		}else{
			$this->error('更新失败');
		}
	}

	/**
	 * 店员列表
	 */
	public function saler(){
		$key = $this->request->param('key/s');
		$status = $this->request->param('status/d',-1);

		$where = [];
		$map = [];
		$query = [];
		if(!empty($key)){
			$where[] = ['salername','like','%'.$key.'%'];
			$query['key'] = $key;
		}

		if($status >= 0){
			$map['status'] = $status;
			$query['status'] = $status;
		}

		$list = SalerModel::with('store')->where($map)->where($where)->order('id desc')->paginate(15, false, ['query' => $query]);

		$this->assign('list', $list);
		return $this->fetch();
	}

	/**
	 * 添加店员
	 */
	public function addSaler(){
		if($this->request->isPost()){
			$data = $this->request->post();
			//验证
			$result = $this->validate($data,[
				'store_id|所属门店' => 'require',
				'salername|店员姓名' => 'require'
			]);
			// 验证失败 输出错误信息
			if(true !== $result) $this->error($result);

			if($res = SalerModel::create($data)){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}else{
			$store = StoreModel::where('status',1)->select();

			$this->assign('info', null);
			$this->assign('store', $store);

			return $this->fetch('editSaler');
		}
	}

	/**
	 * 编辑店员
	 * @param  integer $id 
	 * @return mixed
	 */
	public function editSaler($id = 0){
		if($this->request->isPost()){
			$data = $this->request->post();
			//验证
			$result = $this->validate($data,[
				'store_id|所属门店' => 'require',
				'salername|店员姓名' => 'require'
			]);
			// 验证失败 输出错误信息
			if(true !== $result) $this->error($result);

			if($res = SalerModel::update($data)){
				$this->success('编辑成功');
			}else{
				$this->error('编辑失败');
			}
		}else{
			empty($id) && $this->error('参数错误');

			$info = SalerModel::where('id',$id)->find();
			$store = StoreModel::where('status',1)->select();

			$this->assign('info', $info);
			$this->assign('store', $store);

			return $this->fetch('editSaler');
		}
	}

	/**
	 * 删除店员
	 * @param  integer $id 
	 * @return mixed
	 */
	public function delSaler($id = 0){
		empty($id) && $this->error('参数错误');

		if(is_array($id)){
			$map[] = ['id','in',$id];
		}elseif(is_numeric($id)){
			$map[] = ['id','=',$id];
		}

		if($query = SalerModel::where($map)->delete()){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

	/**
	 * 更新店员
	 * @param  integer $id
	 * @return mixed
	 */
	public function updateSaler($id = 0){
		$data = $this->request->param();

		if(empty($data)){
			$this->error('参数错误');
		}	
		
		if($query = SalerModel::update($data)){
			$this->success('更新成功');
		}else{
			$this->error('更新失败');
		}
	}

	/**
	 * 核销记录
	 * @return mixed
	 */
	public function order(){
		$salername = $this->request->param('salername');
		$store_id = $this->request->param('store_id',0);

		$map = [];
		$where = [];
		$query = [];
		if(!empty($salername)){
			$map[] = ['salername','like','%'.$salername.'%'];
			$query['salername'] = $salername;
		}

		if($store_id > 0){
			$where['store_id'] = $store_id;
			$query['store_id'] = $store_id;
		}

		$store = StoreModel::where('status',1)->select();
		$list = OrderModel::with('store')->where($map)->where($where)->order('id desc')->paginate(15, false, ['query' => $query]);


		$this->assign('store', $store);
		$this->assign('list', $list);
		return $this->fetch();
	}

	/**
	 * 删除核销记录
	 * @param  integer $id 
	 * @return mixed
	 */
	public function delOrder($id = 0){
		empty($id) && $this->error('参数错误');

		if(is_array($id)){
			$map[] = ['id','in',$id];
		}elseif(is_numeric($id)){
			$map[] = ['id','=',$id];
		}

		if($query = OrderModel::where($map)->delete()){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

	/**
	 * 地图
	 * @return array
	 */
	public function map(){
		return $this->fetch();
	}
}
